//
// Created by SDSC\202100300261 on 23-11-12.
//

#include "sym.h"
#include "BLOCK.h"
#include "CodeTable.h"
#include "Runner.h"

int main() {
    /**
     * 选择要读取的代码
     * pl0code0: 输入n，输出1+2+...+n
     * pl0code1: 输入n，输出7 + n * 2 * (2 + 3) 用于测试表达式解析
     * pl0code2: 测试过程调用
     * pl0code3: 输入n，输出斐波那契数列前n项
     */
    std::string s = "./input/pl0code3";

    std::ofstream outputTokens("./output/tokens");

    /**
     * 词法分析
     */
    std::vector<TOKEN*>* tokens = getSYM(s);

    for(auto token:*tokens) {
        outputTokens<<*token;
    }
    outputTokens.close();

    /**
     * 语法分析
     */
    std::ofstream outputTable("./output/table");
    std::ofstream outputCode("./output/code");
    auto *codeTable = new CodeTable();
    auto *block = new BLOCK(tokens,codeTable);
    block -> execMain();
    outputTable<<*(block->table);
    outputCode<<*codeTable;
    outputTable.close();
    outputCode.close();
    /**
     * 代码执行
     */
    Runner* runner = new Runner(codeTable);
    runner -> run();

    delete runner;
    delete tokens;
    delete codeTable;
    delete block;
    return 0;
}